//
//  MeetingSwitchPageViewDelegateSource.m
//  RongRTCMeeting
//
//  Created by LiuLinhong on 2017/08/28.
//  Copyright © 2017年 Beijing Rongcloud Network Technology Co. , Ltd. All rights reserved.
//

#import "MeetingSwitchPageViewDelegateSource.h"
#import "MeetingViewController.h"
#import "SwitchPageViewCell.h"
#import "AppDelegate.h"
#import "CommonUtility.h"
#import "MeetingVideoViewModel.h"

@interface MeetingSwitchPageViewDelegateSource ()
{
    NSString *originalRemoteUserID;
    MeetingVideoViewModel *originalSelectedViewModel;
    NSInteger orignalRow;
    SwitchPageViewCell *tmpRemoteVideoCell;
}
@property (nonatomic, strong) MeetingViewController *meetingViewController;

@end


@implementation MeetingSwitchPageViewDelegateSource

- (instancetype)initWithViewController:(NSViewController *)vc
{
    self = [super init];
    if (self)
    {
        self.meetingViewController = (MeetingViewController *) vc;
    }
    return self;
}

#pragma mark - SwitchPageViewControlDelegate
- (NSEdgeInsets)switchPageViewControl:(SwitchPageViewControl *_Nonnull)switchPageViewControl insetOfCellAtIndex:(NSIndexPath *_Nonnull)indexPath
{
    return NSEdgeInsetsMake(0, 0, 0, 6);
}

- (void)switchPageViewControl:(SwitchPageViewControl *_Nonnull)switchPageViewControl didSelectedCellAtIndexPath:(NSIndexPath *_Nonnull)indexPath
{
    NSInteger page = indexPath.section;
    NSInteger cell = indexPath.item;
    DLog(@"LLH...... didSelectCellAtIndexPath page: %zd  cell: %zd", page, cell);
    NSInteger cIndex = (page * self.meetingViewController.meetingViewBuilder.switchPageViewControl.cellNumberInFullPage) + cell;
    SwitchPageViewCell *remoteVideoCell = (SwitchPageViewCell *)switchPageViewControl.remoteVideoCellArray[cell];
    MeetingVideoViewModel *selectedViewModel = (MeetingVideoViewModel *)LarkMeetingManager.remoteVideoViewArray[cIndex];
    self.meetingViewController.selectedViewModel = selectedViewModel;
    CGRect videoViewRect = CGRectMake(0, 0, kVideoWidth, kVideoHeight);
    if (LarkMeetingManager.isFullScreen)
        videoViewRect = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    
    if (LarkMeetingManager.isSwitchVideoView)
    {
        if (cell == LarkMeetingManager.selectedSwitchCellIndex && page == LarkMeetingManager.selectedSwitchPageIndex)
        {
            //本地
            if (LarkMeetingManager.observerIndex == RongRTC_User_Observer)
                self.meetingViewController.localVideoViewModel.cellVideoView = [self.meetingViewController.rongRTCEngine changeRemoteVideoViewFrame:videoViewRect withUserID:self.meetingViewController.localVideoViewModel.userID withDisplayType:RongRTC_VideoViewDisplay_CompleteView];
            else
                self.meetingViewController.localVideoViewModel.cellVideoView = [self.meetingViewController.rongRTCEngine changeLocalVideoViewFrame:videoViewRect withDisplayType:RongRTC_VideoViewDisplay_CompleteView];
            
//            NSView *localVideoView = [[RongRTCEngine sharedRongRTCEngine] changeLocalVideoViewFrame:videoViewRect withDisplayType:RongRTC_VideoViewDisplay_CompleteView];
            AppDelegate *appDelegate = (AppDelegate *)[NSApplication sharedApplication].delegate;
            [appDelegate.childWindow.contentView addSubview:self.meetingViewController.localVideoViewModel.cellVideoView];
            
            if (self.meetingViewController.localVideoViewModel.avType == RongRTC_User_Only_Audio || self.meetingViewController.localVideoViewModel.avType == RongRTC_User_Audio_Video_None)
            {
                self.meetingViewController.localVideoViewModel.micImageView.frame = CGRectMake((kVideoWidth-kCellSize)/2, (kVideoHeight-kCellSize)/2, self.meetingViewController.localVideoViewModel.micImageView.frame.size.width, self.meetingViewController.localVideoViewModel.micImageView.frame.size.height);
                
                [appDelegate.childWindow.contentView addSubview:self.meetingViewController.localVideoViewModel.micImageView];
            }
            
            if (LarkMeetingManager.is4ShareScreen)
            {
                self.meetingViewController.meetingViewBuilder.screenShareMessageTextField.frame = CGRectMake(0, (self.meetingViewController.localVideoViewModel.cellVideoView.frame.size.height - 20)/2, self.meetingViewController.localVideoViewModel.cellVideoView.frame.size.width, 20);
                self.meetingViewController.meetingViewBuilder.screenShareMessageTextField.font = [NSFont systemFontOfSize:18.0];
            }
            
            //远端
            NSView *remoteVideoView = [[RongRTCEngine sharedRongRTCEngine] changeRemoteVideoViewFrame:CGRectMake(0, 0, kCellSize, kCellSize) withUserID:remoteVideoCell.userID withDisplayType:RongRTC_VideoViewDisplay_FullScreen];
            [remoteVideoCell addSubview:remoteVideoView];
            selectedViewModel.cellVideoView = remoteVideoView;
            
            if (selectedViewModel.avType == RongRTC_User_Only_Audio || selectedViewModel.avType == RongRTC_User_Audio_Video_None)
            {
                selectedViewModel.micImageView.frame = CGRectMake(0, 0, selectedViewModel.micImageView.frame.size.width, selectedViewModel.micImageView.frame.size.height);
                
                [remoteVideoView addSubview:selectedViewModel.micImageView];
            }

            LarkMeetingManager.selectedSwitchCellIndex = -1;
            LarkMeetingManager.selectedSwitchPageIndex = -1;
            LarkMeetingManager.isSwitchVideoView = !LarkMeetingManager.isSwitchVideoView;
            
            [self.meetingViewController.rongRTCEngine subscribeStreamForTiny:LarkMeetingManager.userIDArray forOrigin:nil];
        }
        else
        {
            //之前在小图的远端
            NSView *tmpRemoteVideoView = [[RongRTCEngine sharedRongRTCEngine] changeRemoteVideoViewFrame:CGRectMake(0, 0, kCellSize, kCellSize) withUserID:tmpRemoteVideoCell.userID withDisplayType:RongRTC_VideoViewDisplay_FullScreen];
            [tmpRemoteVideoCell addSubview:tmpRemoteVideoView];
            originalSelectedViewModel.cellVideoView = tmpRemoteVideoView;
            
            if (originalSelectedViewModel.avType == RongRTC_User_Only_Audio || originalSelectedViewModel.avType == RongRTC_User_Audio_Video_None)
            {
                originalSelectedViewModel.micImageView.frame = CGRectMake(0, 0, originalSelectedViewModel.micImageView.frame.size.width, originalSelectedViewModel.micImageView.frame.size.height);
                
                [tmpRemoteVideoView addSubview:selectedViewModel.micImageView];
            }
            
            //远端
            originalSelectedViewModel = selectedViewModel;
            NSView *remoteVideoView = [[RongRTCEngine sharedRongRTCEngine] changeRemoteVideoViewFrame:videoViewRect withUserID:remoteVideoCell.userID withDisplayType:RongRTC_VideoViewDisplay_CompleteView];
            AppDelegate *appDelegate = (AppDelegate *)[NSApplication sharedApplication].delegate;
            [appDelegate.childWindow.contentView addSubview:remoteVideoView];
            selectedViewModel.cellVideoView = remoteVideoView;
            
            if (selectedViewModel.avType == RongRTC_User_Only_Audio || selectedViewModel.avType == RongRTC_User_Audio_Video_None)
            {
                if (LarkMeetingManager.isFullScreen)
                {
                    selectedViewModel.micImageView.frame = CGRectMake((kScreenWidth-kCellSize)/2, (kScreenHeight-kCellSize)/2, selectedViewModel.micImageView.frame.size.width, selectedViewModel.micImageView.frame.size.height);
                }
                else
                {
                    selectedViewModel.micImageView.frame = CGRectMake((kVideoWidth-kCellSize)/2, (kVideoHeight-kCellSize)/2, selectedViewModel.micImageView.frame.size.width, selectedViewModel.micImageView.frame.size.height);
                }
                
                [remoteVideoView addSubview:selectedViewModel.micImageView];
            }
            
            //本地
            if ([self.meetingViewController.localVideoViewModel.userID isEqualToString:self.meetingViewController.userID])
            {
                self.meetingViewController.localVideoViewModel.cellVideoView = [self.meetingViewController.rongRTCEngine changeLocalVideoViewFrame:CGRectMake(0, 0, kCellSize, kCellSize) withDisplayType:RongRTC_VideoViewDisplay_FullScreen];
            }
            else
            {
                self.meetingViewController.localVideoViewModel.cellVideoView = [self.meetingViewController.rongRTCEngine changeRemoteVideoViewFrame:CGRectMake(0, 0, kCellSize, kCellSize) withUserID:self.meetingViewController.localVideoViewModel.userID withDisplayType:RongRTC_VideoViewDisplay_FullScreen];
            }
            [remoteVideoCell addSubview:self.meetingViewController.localVideoViewModel.cellVideoView];
            
            if (self.meetingViewController.localVideoViewModel.avType == RongRTC_User_Only_Audio || self.meetingViewController.localVideoViewModel.avType == RongRTC_User_Audio_Video_None)
            {
                self.meetingViewController.localVideoViewModel.micImageView.frame = CGRectMake(0, 0, self.meetingViewController.localVideoViewModel.micImageView.frame.size.width, self.meetingViewController.localVideoViewModel.micImageView.frame.size.height);
                
                [remoteVideoCell addSubview:self.meetingViewController.localVideoViewModel.micImageView];
            }
            
            if (LarkMeetingManager.is4ShareScreen)
            {
                self.meetingViewController.meetingViewBuilder.screenShareMessageTextField.frame = CGRectMake(0, (self.meetingViewController.localVideoViewModel.cellVideoView.frame.size.height - 20)/2, self.meetingViewController.localVideoViewModel.cellVideoView.frame.size.width, 20);
                self.meetingViewController.meetingViewBuilder.screenShareMessageTextField.font = [NSFont systemFontOfSize:16.0];
            }
            
            tmpRemoteVideoCell = remoteVideoCell;
            LarkMeetingManager.selectedSwitchCellIndex = cell;
            LarkMeetingManager.selectedSwitchPageIndex = page;
            
            NSString *bigStrUserID = remoteVideoCell.userID;
            NSMutableArray *tinyStreamUseIDs = [NSMutableArray arrayWithArray:LarkMeetingManager.userIDArray];
            [tinyStreamUseIDs removeObject:bigStrUserID];
            [self.meetingViewController.rongRTCEngine subscribeStreamForTiny:tinyStreamUseIDs forOrigin:bigStrUserID];
        }
    }
    else
    {
        originalSelectedViewModel = selectedViewModel;
        originalRemoteUserID = selectedViewModel.userID;
        
        //远端
        NSView *remoteVideoView = [[RongRTCEngine sharedRongRTCEngine] changeRemoteVideoViewFrame:videoViewRect withUserID:remoteVideoCell.userID withDisplayType:RongRTC_VideoViewDisplay_CompleteView];
        AppDelegate *appDelegate = (AppDelegate *)[NSApplication sharedApplication].delegate;
        [appDelegate.childWindow.contentView addSubview:remoteVideoView];
        selectedViewModel.cellVideoView = remoteVideoView;
        
        if (selectedViewModel.avType == RongRTC_User_Only_Audio || selectedViewModel.avType == RongRTC_User_Audio_Video_None)
        {
            if (LarkMeetingManager.isFullScreen)
            {
                selectedViewModel.micImageView.frame = CGRectMake((kScreenWidth-kCellSize)/2, (kScreenHeight-kCellSize)/2, selectedViewModel.micImageView.frame.size.width, selectedViewModel.micImageView.frame.size.height);
            }
            else
            {
                selectedViewModel.micImageView.frame = CGRectMake((kVideoWidth-kCellSize)/2, (kVideoHeight-kCellSize)/2, selectedViewModel.micImageView.frame.size.width, selectedViewModel.micImageView.frame.size.height);
            }
            
            [remoteVideoView addSubview:selectedViewModel.micImageView];
        }
        
        //本地
        if ([self.meetingViewController.localVideoViewModel.userID isEqualToString:self.meetingViewController.userID])
        {
            self.meetingViewController.localVideoViewModel.cellVideoView = [self.meetingViewController.rongRTCEngine changeLocalVideoViewFrame:CGRectMake(0, 0, kCellSize, kCellSize) withDisplayType:RongRTC_VideoViewDisplay_FullScreen];
        }
        else
        {
            self.meetingViewController.localVideoViewModel.cellVideoView = [self.meetingViewController.rongRTCEngine changeRemoteVideoViewFrame:CGRectMake(0, 0, kCellSize, kCellSize) withUserID:self.meetingViewController.localVideoViewModel.userID withDisplayType:RongRTC_VideoViewDisplay_FullScreen];
        }
        [remoteVideoCell addSubview:self.meetingViewController.localVideoViewModel.cellVideoView];
        
        if (self.meetingViewController.localVideoViewModel.avType == RongRTC_User_Only_Audio || self.meetingViewController.localVideoViewModel.avType == RongRTC_User_Audio_Video_None)
        {
            self.meetingViewController.localVideoViewModel.micImageView.frame = CGRectMake(0, 0, self.meetingViewController.localVideoViewModel.micImageView.frame.size.width, self.meetingViewController.localVideoViewModel.micImageView.frame.size.height);
            
            [remoteVideoCell addSubview:self.meetingViewController.localVideoViewModel.micImageView];
        }
        
        if (LarkMeetingManager.is4ShareScreen)
        {
            self.meetingViewController.meetingViewBuilder.screenShareMessageTextField.frame = CGRectMake(0, (self.meetingViewController.localVideoViewModel.cellVideoView.frame.size.height - 20)/2, self.meetingViewController.localVideoViewModel.cellVideoView.frame.size.width, 20);
            self.meetingViewController.meetingViewBuilder.screenShareMessageTextField.font = [NSFont systemFontOfSize:16.0];
        }
        
        tmpRemoteVideoCell = remoteVideoCell;
        LarkMeetingManager.isSwitchVideoView = !LarkMeetingManager.isSwitchVideoView;
        LarkMeetingManager.selectedSwitchCellIndex = cell;
        LarkMeetingManager.selectedSwitchPageIndex = page;
        
        NSString *bigStrUserID = remoteVideoCell.userID;
        NSMutableArray *tinyStreamUseIDs = [NSMutableArray arrayWithArray:LarkMeetingManager.userIDArray];
        [tinyStreamUseIDs removeObject:bigStrUserID];
        [self.meetingViewController.rongRTCEngine subscribeStreamForTiny:tinyStreamUseIDs forOrigin:bigStrUserID];
    }
}

#pragma mark - SwitchPageViewControlDateSource
- (NSInteger)switchPageViewControl:(SwitchPageViewControl *_Nonnull)switchPageViewControl numberOfCellsInSwitchPageView:(NSInteger)pageIndex
{
    NSInteger count = [LarkMeetingManager.remoteVideoViewArray count];
    
    if ((pageIndex + 1) * self.meetingViewController.meetingViewBuilder.switchPageViewControl.cellNumberInFullPage <= count)
        return self.meetingViewController.meetingViewBuilder.switchPageViewControl.cellNumberInFullPage;
    else
        return fmod(count, self.meetingViewController.meetingViewBuilder.switchPageViewControl.cellNumberInFullPage);
}

- (NSDictionary *_Nonnull)switchPageViewControl:(SwitchPageViewControl *_Nonnull)switchPageViewControl cellViewForIndex:(NSIndexPath *_Nonnull)indexPath
{
    NSInteger page = indexPath.section;
    NSInteger cell = indexPath.item;
    NSInteger index = (page * self.meetingViewController.meetingViewBuilder.switchPageViewControl.cellNumberInFullPage) + cell;
    
    MeetingVideoViewModel *model = (MeetingVideoViewModel *)LarkMeetingManager.remoteVideoViewArray[index];
//    NSDictionary *dic = @{kUserID:model.userID, kRemoteVideoView:model.cellVideoView, kUserType:@(model.userType), kAudioVideoType:@(model.avType)};
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    
    if (model.userID)
        dic[kUserID] = model.userID;
    
    dic[kUserType] = @(model.userType);
    dic[kAudioVideoType] = @(model.avType);
    dic[kSwitchHeadIcon] = model.switchHeadIconView;
    
    if (model.cellVideoView)
        dic[kRemoteVideoView] = model.cellVideoView;
    
    return dic;
}

- (NSInteger)numberOfPagesInSwitchPageViewControl:(SwitchPageViewControl *_Nonnull)switchPageViewControl
{
    CGFloat count = [LarkMeetingManager.remoteVideoViewArray count];
    NSInteger totalPage = ceil(count / self.meetingViewController.meetingViewBuilder.switchPageViewControl.cellNumberInFullPage);
    return totalPage;
}

@end
